package com.cetcs.kmga.dataView.controller;

import com.cetcs.kmga.common.BaseController;
import com.cetcs.kmga.common.BaseResponse;
import com.cetcs.kmga.common.Constant;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.dataView.dao.DataSourceContextHolder;
import com.cetcs.kmga.dataView.entity.cloudDataView.DataInOutNum;
import com.cetcs.kmga.dataView.entity.cloudDataView.vo.*;
import com.cetcs.kmga.dataView.service.ICloudDataViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/1/24 0024.
 */
@RestController
@RequestMapping("/cloudDataView")
public class CloudDataViewController extends BaseController {

    @Autowired
    private ICloudDataViewService cloudDataViewService;

    //云平台系统资源总览
    @RequestMapping(value = "/viewCloudDataSystems", method = RequestMethod.GET)
    public BaseResponse viewCloudDataSystems(WebRequest request) {
        try {
            Data params = getParam(request);
            //数据库切换
            DataSourceContextHolder.setDataSourceType("cddataSource");
            List<CloudDataSystemsVo> result = cloudDataViewService.viewCloudDataSystems(params);
            //DataSourceContextHolder.setDataSourceType("bddataSource");
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    //云平台系统资源总览
    @RequestMapping(value = "/viewCloudDataSystemsInfo", method = RequestMethod.GET)
    public BaseResponse viewCloudDataSystemsInfo(WebRequest request) {
        try {
            Data params = getParam(request);
            //数据库切换
            DataSourceContextHolder.setDataSourceType("cddataSource");
            CloudDataSystemsVo result = cloudDataViewService.viewCloudDataSystemsInfo(params);
            if ("cfc8bba9-bb30-11e7-89d7-0caf485aed97".equals(params.getString("id"))){
                DataSourceContextHolder.setDataSourceType("bddataSource");

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD");

//                params.put("startTime",sdf.format(new Date())+" 01");
//                params.put("endTime",sdf.format(new Date())+" 23");

                params.put("startTime","2018-01-21 01");
                params.put("endTime","2018-01-21 23");
                DataInOutNum dataInOutNum = cloudDataViewService.viewDataInOutNum(params);

                result.setIn(dataInOutNum.getInNum());
                result.setOut(dataInOutNum.getOutNum());
                return this.ok(result);
            }
            else{
                return this.ok(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    //云平台设备总览
    @RequestMapping(value = "/viewCloudDataSources", method = RequestMethod.GET)
    public BaseResponse viewCloudDataSources(WebRequest request) {
        try {
            Data params = getParam(request);
            //数据库切换
            DataSourceContextHolder.setDataSourceType("cddataSource");
            List<CloudDataSourcesVo> result = cloudDataViewService.viewCloudDataSources(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    //云平台气象图
    @RequestMapping(value = "/viewCloudDataWeather", method = RequestMethod.GET)
    public BaseResponse viewCloudDataWeather(WebRequest request) {
        try {
            Data params = getParam(request);
            //数据库切换
            DataSourceContextHolder.setDataSourceType("cddataSource");
            CloudDataWeatherVo result = cloudDataViewService.viewCloudDataWeather(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    //云平台资源状态
    @RequestMapping(value = "/viewCloudDataSituational", method = RequestMethod.GET)
    public BaseResponse viewCloudDataSituational(WebRequest request) {
        try {
            Data params = getParam(request);
            //数据库切换
            DataSourceContextHolder.setDataSourceType("cddataSource");
            List<CloudDataSituationalVo> result = cloudDataViewService.viewCloudDataSituational(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    //云平台负载图
    @RequestMapping(value = "/viewCloudDataWorkLoad", method = RequestMethod.GET)
    public BaseResponse viewCloudDataWorkLoad(WebRequest request) {
        try {
            Data params = getParam(request);
            //数据库切换
            DataSourceContextHolder.setDataSourceType("cddataSource");
            CloudDataWorkLoadVo result = cloudDataViewService.viewCloudDataWorkLoad(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


}
